/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.*;
import javax.persistence.Query;

/**
 *
 * @author shinji
 */
@Stateless
public class FriendFacade extends AbstractFacade<Friend> {
    @PersistenceContext(unitName = "Matrimony-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public FriendFacade() {
        super(Friend.class);
    }
    
    public Friend GetFriendByMemberID(int id1, int id2){
        try {
            Query q = this.em.createQuery("select f from Friend f where f.iDFriend.id = :id1 and f.iDMember.id = :id2 and f.request = TRUE");
            q.setParameter("id1", id1);
            q.setParameter("id2", id2);                 
            List<Friend> LF = q.getResultList();
            q = this.em.createQuery("select f from Friend f where f.iDMember.id = :id1 and f.iDFriend.id = :id2 and f.request = TRUE");
            q.setParameter("id1", id1);
            q.setParameter("id2", id2);
            LF.addAll(q.getResultList());
                       
            return LF.size() > 0 ? LF.get(0) : null;
        } catch (Exception e) {
            return null;
        }
    }
}
